package com.everflourish.yeah100.act.test;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.everflourish.yeah100.R;
import com.everflourish.yeah100.act.base.BaseActivity;
import com.everflourish.yeah100.entity.Examination;
import com.everflourish.yeah100.entity.marking.ErrorExamitation;
import com.everflourish.yeah100.http.MarkingSystemRequest;
import com.everflourish.yeah100.utils.IntentUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/9.
 */

public class ExpendActivity extends BaseActivity {

    private RecyclerView mylist;
    public static Examination mExamination;

    private List<TopicBean> datas;

    private TextAdpater mTextAdpater;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expendlist);

        System.out.print("1235");
        initView();

        reflashData();
    }

    private void initView() {
        datas = new ArrayList<>();
        mylist = (RecyclerView) findViewById(R.id.mylist);
        mTextAdpater = new TextAdpater(this, datas);
        mylist.setLayoutManager(new LinearLayoutManager(this));
        mylist.setAdapter(mTextAdpater);

        mExamination = (Examination) getIntent().getSerializableExtra(
                IntentUtil.EXAMINATION);
    }

    private void reflashData() {
        mQueue.add(MarkingSystemRequest.getInstance().getTopicTypeList(mExamination.getId(), new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                try {
                    Type type = new TypeToken<List<TopicBean>>() {
                    }.getType();

                    if (jsonObject.getString("resultCode").equals("000000I")) {
                        List<TopicBean> topics = gson.fromJson(jsonObject.getJSONArray("topics").toString(), type);
                        List<TopicBean> allOrmSubTopics = gson.fromJson(jsonObject.getJSONArray("topics").toString(), type);

                        if (topics != null)
                            datas.addAll(topics);
                        if (allOrmSubTopics !=null)
                            datas.addAll(allOrmSubTopics);

                        mTextAdpater.notifyDataSetChanged();

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.i("AAA", volleyError.toString());
            }
        }));

    }
}
